@expressive-code/plugin-frames
Contents
What is this?
A default plugin of Expressive Code, an engine for presenting source code on the web.
It renders a window frame around every code block. Depending on the code's language, this frame can look like a code editor (similar to VS Code), or like a terminal window.
Frames can have optional titles, which are either taken from the code block's meta string, or from a file name comment in the first lines of the code.
When should I use this?
You can use this plugin to render a window frame around your code blocks. For more information, see the usage examples below.
This plugin is installed by default by our higher-level packages like remark-expressive-code
, so you can start using it in markdown / MDX documents without having to install it first.
Installation (not required)
No installation is required. This package is installed by default by our higher-level packages.
If you are using the core package directly (e.g. because you are writing an integration), see the Advanced use cases section for more information.
Usage in markdown / MDX documents
If you are using a higher-level integration package like remark-expressive-code
, frames will automatically be rendered around your code blocks in markdown / MDX documents.
The type of frame that will be rendered (editor window or terminal window) is selected automatically based on the language identifier in your code block's opening fence:
Code editor window frames
Code blocks will be rendered as a code editor window if their language identifier is not a terminal language (see next section for a list of terminal languages):
```js
console.log('Hello World!')
```
Terminal window frames
Code blocks will be rendered as a terminal window if their language identifier matches one of the supported terminal languages bash
, shellscript
, shell
, sh
, or zsh
:
```bash
npm install
```
Adding titles (open file tab or terminal window title)
You can give your frames a title by adding an optional title="...your title..."
attribute after the language identifier.
The following code block will be rendered as an editor window with an open file tab named my-test-file.js
:
```js title="my-test-file.js"
console.log('Hello World!')
```
Unless turned off in the plugin configuration, you can also add a file name comment in the first lines of your code to set the title. This comment will be removed from the code and shown as the frame's title instead:
```js
// my-test-file.js
console.log('Hello World!')
```
Overriding frame types
By default, the plugin will automatically select the frame type (code editor or terminal) based on the language identifier in your code block's opening fence.
You can override this behavior and force a specific frame type by adding an optional frame="..."
attribute after the language identifier.
The supported values for this attribute are code
, terminal
, none
and auto
. The default value is auto
.
You can use this feature to render a code block using a shell language (e.g. sh
) as a code editor instead of the default terminal window:
```sh frame="code"
#!/bin/sh
# my-script.sh
echo "This is a script file, not a terminal!"
```
The special frame type none
will always render a plain code block without a frame or title, and leave any file name comments in the code untouched.
Configuration
When using this plugin through higher-level integration packages, you can configure it by passing options to the higher-level package.
Here are configuration examples for some popular site generators:
Astro configuration example
We assume that you're using our Astro integration astro-expressive-code
.
In your Astro config file, you can pass options to the frames plugin like this:
import { defineConfig } from 'astro/config'
import astroExpressiveCode from 'astro-expressive-code'
const astroExpressiveCodeOptions = {
frames: {
styleOverrides: {
editorTabBarBackground: ({ theme }) => theme.colors['menu.background'],
shadowColor: 'purple',
},
},
}
export default defineConfig({
integrations: [
astroExpressiveCode(astroExpressiveCodeOptions),
],
})
Next.js configuration example using @next/mdx
import createMDX from '@next/mdx'
import remarkExpressiveCode from 'remark-expressive-code'
const remarkExpressiveCodeOptions = {
frames: {
styleOverrides: {
editorTabBarBackground: ({ theme }) => theme.colors['menu.background'],
shadowColor: 'purple',
},
},
}
const nextConfig = {
reactStrictMode: true,
pageExtensions: ["js", "jsx", "ts", "tsx", "md", "mdx"],
}
const withMDX = createMDX({
extension: /\.mdx?$/,
options: {
remarkPlugins: [
[remarkExpressiveCode, remarkExpressiveCodeOptions],
],
rehypePlugins: [],
},
})
export default withMDX(nextConfig)
Available plugin options
You can pass the following options to the plugin:
-
extractFileNameFromCode: boolean
If true
(which is the default), and no title was found in the code block's meta string, the plugin will try to find and extract a comment line containing the code block file name from the first 4 lines of the code.
-
showCopyToClipboardButton: boolean
If true
(which is the default), a "Copy to clipboard" button will be shown for each code block.
-
removeCommentsWhenCopyingTerminalFrames: boolean
If true
(which is the default), the "Copy to clipboard" button of terminal window frames will remove comment lines starting with #
from the copied text.
This is useful to reduce the copied text to the actual commands users need to run, instead of also copying explanatory comments or instructions.
-
styleOverrides
Allows overriding the plugin's default styles using an object with named properties.
The property values can either be a string, or a function that returns a string. If a function is used, it will be called with the following arguments:
-
theme
: An ExpressiveCodeTheme object containing the current theme's colors and other properties.
-
coreStyles
: An object containing the ExpressiveCodeEngine core styles.
-
resolveSetting
: A function that can be used to resolve another style setting. It takes a style property name, and returns its resolved value. For example, frameBoxShadowCssValue
uses it to include the shadowColor
value:
styleOverrides: {
frameBoxShadowCssValue: ({ resolveSetting }) =>
`0.1rem 0.1rem 0.2rem ${resolveSetting('shadowColor')}`
}
The following properties are available:
-
General styles: shadowColor
, frameBoxShadowCssValue
-
Editor styles:
editorActiveTabBackground
, editorActiveTabForeground
, editorActiveTabBorder
, editorActiveTabBorderTop
, editorActiveTabBorderBottom
, editorTabBorderRadius
, editorTabBarBackground
, editorTabBarBorderColor
, editorTabBarBorderBottom
, editorBackground
-
Terminal styles:
terminalTitlebarDotsForeground
, terminalTitlebarBackground
, terminalTitlebarForeground
, terminalTitlebarBorderBottom
, terminalBackground
Advanced use cases
Manual installation
You only need to install this plugin if you are using the core package @expressive-code/core
directly. In this case, you can install the plugin like this:
npm install @expressive-code/plugin-frames
Manual usage from the core package
Warning:
This is an advanced usage example! You normally don't need to use the core package directly, or manually add this plugin to the configuration.
import { ExpressiveCodeEngine } from '@expressive-code/core'
import { pluginFrames } from '@expressive-code/plugin-frames'
const ec = new ExpressiveCodeEngine({
plugins: [
pluginFrames(),
],
})
const code = `
// my-test-file.js
const hello = 'World!'
`
const renderResult = await ec.render({
code: code.trim(),
language: 'js',
})
console.log(renderResult.renderedGroupContents[0].codeBlock.code)